from django.shortcuts import render
from web import models
from utils.pager import Pagination
from utils.group import NbSearchGroup,Option
from django.db.models import Q

def my_transaction_list(request):
    search_group = NbSearchGroup(
        request,
        models.TransactionRecord,
        Option('charge_type',db_condition={'active':1})
    )

    keyword = request.GET.get('keyword','').strip()
    con = Q()
    if keyword:
        con.connector = 'QR'
        con.children.append(('order_oid__contains', keyword))

    queryset = models.TransactionRecord.objects.filter(con).filter(**search_group.get_condition).filter(customer_id=request.fc_user.id,active=1).order_by('-id')
    pager = Pagination(request,queryset)
    context ={
        'pager': pager,
        'search_group': search_group,
        'keyword': keyword,
    }
    return render(request,'my_transaction_list.html',context)

def transaction_list(request):
    search_group = NbSearchGroup(
        request,
        models.TransactionRecord,
        Option('charge_type')
    )
    keyword = request.GET.get('keyword','').strip()
    con = Q()
    if keyword:
        con.connector = 'QR'
        con.children.append(('order_oid__contains', keyword))
        con.children.append(('customer__username__contains', keyword))

    queryset = models.TransactionRecord.objects.filter(con).filter(**search_group.get_condition).filter(active=1).order_by('-id')
    pager = Pagination(request,queryset)
    context ={
        'pager': pager,
        'search_group': search_group,
        'keyword': keyword,
    }
    return render(request,'transaction_list.html',context)